// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.DirectoryStream.Filter;
import java.util.ArrayList;
import zombie.core.logger.ExceptionLogger;

public class ServerSettingsManager {
    public static final ServerSettingsManager instance = new ServerSettingsManager();
    protected ArrayList<ServerSettings> settings = new ArrayList();
    protected ArrayList<String> suffixes = new ArrayList();

    public String getSettingsFolder() {
        return zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + "Server";
    }

    public String getNameInSettingsFolder(String name) {
        return this.getSettingsFolder() + File.separator + name;
    }

    public void readAllSettings() {
        this.settings.clear();
        File file = new File(this.getSettingsFolder());
        if (!file.exists()) {
            file.mkdirs();
        } else {
            Filter filter = new Filter<Path>() {
                public boolean accept(Path path) throws IOException {
                    String string = path.getFileName().toString();
                    return !Files.isDirectory(path, new LinkOption[0])
                        && string.endsWith(".ini")
                        && !string.endsWith("_zombies.ini")
                        && ServerSettingsManager.this.isValidName(string.replace(".ini", ""));
                }
            };

            try {
                DirectoryStream directoryStream = Files.newDirectoryStream(file.toPath(), filter);

                try {
                    for (Path path : directoryStream) {
                        ServerSettings serverSettings = new ServerSettings(path.getFileName().toString().replace(".ini", ""));
                        this.settings.add(serverSettings);
                    }
                } catch (Throwable throwable0) {
                    if (directoryStream != null) {
                        try {
                            directoryStream.close();
                        } catch (Throwable throwable1) {
                            throwable0.addSuppressed(throwable1);
                        }
                    }

                    throw throwable0;
                }

                if (directoryStream != null) {
                    directoryStream.close();
                }
            } catch (Exception exception) {
                ExceptionLogger.logException(exception);
            }
        }
    }

    public int getSettingsCount() {
        return this.settings.size();
    }

    public ServerSettings getSettingsByIndex(int index) {
        return index >= 0 && index < this.settings.size() ? (ServerSettings)this.settings.get(index) : null;
    }

    public boolean isValidName(String name) {
        if (name == null || name.isEmpty()) {
            return false;
        } else {
            return name.contains("/") || name.contains("\\") || name.contains(":") || name.contains(";") || name.contains("\"") || name.contains(".")
                ? false
                : !name.contains("_zombies");
        }
    }

    private boolean anyFilesExist(String string) {
        this.getSuffixes();

        for (int _int = 0; _int < this.suffixes.size(); _int++) {
            File file = new File(this.getSettingsFolder() + File.separator + string + (String)this.suffixes.get(_int));
            if (file.exists()) {
                return true;
            }
        }

        return false;
    }

    public boolean isValidNewName(String newName) {
        return !this.isValidName(newName) ? false : !this.anyFilesExist(newName);
    }

    public ArrayList<String> getSuffixes() {
        if (this.suffixes.isEmpty()) {
            this.suffixes.add(".ini");
            this.suffixes.add("_SandboxVars.lua");
            this.suffixes.add("_spawnpoints.lua");
            this.suffixes.add("_spawnregions.lua");
            this.suffixes.add("_zombies.ini");
        }

        return this.suffixes;
    }
}
